package controllers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import models.BackCount;
import models.Operator;
import services.AuthorityService;
import services.BackCountService;
import services.RejectCountService;
import utils.BizConstants;
import vos.BackCountSearchVo;
import vos.BackCountVo;
import vos.CountMonthVo;
import vos.RejectCountSearchVo;
import vos.ReturnCode;
import vos.ReturnListVo;

/**
 * 驳回返款控制类
 * 
 * @ClassName: RejectCountController 
 * @author: wangsm
 * @date: 2016年6月7日 上午9:11:54
 */
public class RejectCountController extends BaseController {
	/**
	 * 驳回返款
	 *
	 * @Title: rejectcount 
	 * @return: void
	 */
	public static void rejectcount(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取驳回的统计信息
	 *
	 * @Title: getReject 
	 * @return: void
	 */
	public static void getReject(int page,int rows, String sort,String order){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		RejectCountSearchVo vo=new RejectCountSearchVo(page,rows,sort,order);
		long total=RejectCountService.count(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<BackCountVo> backvos=RejectCountService.getReject(vo);
		listVo.setRows(backvos);
		renderJSON(listVo);
	}
	/**
	 * 重新统计
	 *
	 * @Title: reCount 
	 * @return: void
	 */
	public static void reCount(List<CountMonthVo> ids){
		if(!checkAuthority("REJECTCOUNT_RECOUNT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(ids==null){
			renderFailedJson(ReturnCode.ErrorCode,"选中记录为空");
		}
		Operator opr=getLoginUser();
		boolean isok=RejectCountService.reCount(ids,opr.operatorid);
		if(isok){
			renderFailedJson(ReturnCode.SuccessCode, "重新统计成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "重新统计失败");
		}
	}
	/**
	 * 保存重新统计
	 *
	 * @Title: saveReject 
	 * @param ids
	 * @return: void
	 */
	public static void saveReject(String ids){
		if(!checkAuthority("REJECTCOUNT_SAVE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		String[] id=ids.split(",");
		List<Long> listid=new ArrayList<Long>();
		for(int i=0;i<id.length;i++){
			listid.add(Long.parseLong(id[i]));
		}
		Operator opr=getLoginUser();
		boolean isok=RejectCountService.saveReject(listid,opr.operatorid);
		if(isok){
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
}
